﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Negocio;

namespace Persistencia
{
    class BrokerPlayerExecution : Broker
    {
        public BrokerPlayerExecution()
        {
            this.conn = ConnectionDB.getInstance();
        }

        public override void add(Object o)
        {
            PlayerExecution execution = (PlayerExecution)o;
            this.conn.connect();
            ObservableCollection<TestValue> list = execution.Values;
            IEnumerator<TestValue> t = list.GetEnumerator();
            string sql = "insert into PlayerExecution (Date_,EvaluationName,oidJugador,oid,groupName, comments) values('" + execution.Date + "','" + execution.EvaluationName + "'," + execution.Player.OID + "," + execution.OID + ",'" + execution.GroupName + "', '" + execution.Comments + "')";           
            this.conn.execute(sql);
            string sql2 = "update OID set OId=" + execution.OID;
            this.conn.execute(sql2);
            while (t.MoveNext())
            {
                t.Current.added();                
                string sql1 = "update TestValue set OIdexecution =" + execution.OID + " where OId =" + t.Current.OID;
                this.conn.execute(sql1);         
            }
            this.conn.disconnect();
        }

        public override void update(Object o)
        {
            PlayerExecution execution = (PlayerExecution)o;
            this.conn.connect();
            string sql = "update playerExecution set comments = '" + execution.Comments + "' where OId = " + execution.OID;
            this.conn.execute(sql);
            ObservableCollection<TestValue> list = execution.Values;
            IEnumerator<TestValue> t = list.GetEnumerator();
            while (t.MoveNext())
            {
                string sql2 = "update TestValue set value_= '" + ((TestValue)t.Current).ValueExecution + "' where oid =" + ((TestValue)t.Current).OID;
                this.conn.execute(sql2);
            }
            this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            /* Los test no se borran*/
        }


        public override void bringData(Object o)
         { 
             
             PlayerExecution pexec = (PlayerExecution)o;
             string sql = "Select * from PlayerExecution where oid =" + pexec.OID;
             this.conn.bringData(sql);
             while (this.conn.nextRecord())
             {
                 pexec.EvaluationName= this.conn.getDataString("EvaluationName").ToString();
                 pexec.Player.OID = int.Parse(this.conn.getDataString("oidJugador").ToString());
                 pexec.Date =(DateTime)this.conn.getDataString("date_");
                 pexec.OID = int.Parse(this.conn.getDataString("oid").ToString());
                 pexec.GroupName = this.conn.getDataString("groupName").ToString();
                 pexec.Comments = this.conn.getDataString("comments").ToString();
             }
             string sql1 = "Select * from TestValue where oidExecution=" + pexec.OID;
                this.conn.bringList(sql1);
                while (this.conn.nextRecord())
                {
                    TestValue test = new TestValue();
                    test.Test.OID = int.Parse(this.conn.getDataString("oidTest").ToString());
                    test.ValueExecution = int.Parse(this.conn.getDataString("value_").ToString());
                    test.OID = int.Parse(this.conn.getDataString("oid").ToString());
                    pexec.addValue(test);
                                 
             }
             this.conn.disconnect();
            
         }
        public override ObservableCollection<Object> bringList(Object o)
        {
            ObservableCollection<Object> list = new ObservableCollection<Object>();
            string sql = "Select * from PlayerExecution order by date_";
            this.conn.connect();
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {
                PlayerExecution pexec = new PlayerExecution();
                 pexec.EvaluationName= this.conn.getDataString("EvaluationName").ToString();
                 pexec.Player.OID = int.Parse(this.conn.getDataString("oidJugador").ToString());
                 pexec.Date=(DateTime)this.conn.getDataString("date_");
                 pexec.OID = int.Parse(this.conn.getDataString("oid").ToString());
                 pexec.GroupName = this.conn.getDataString("groupName").ToString();
                 pexec.Comments = this.conn.getDataString("comments").ToString();
                 list.Add(pexec);
            }
            IEnumerator<Object> p = list.GetEnumerator();
            while (p.MoveNext())
            {
                string sql1 = "Select t.* from TestValue t where t.oidExecution =" +((PlayerExecution)p.Current).OID;
                this.conn.bringList(sql1);
                while (this.conn.nextRecord())
                {
                    TestValue test = new TestValue();
                    test.Test.OID = int.Parse(this.conn.getDataString("oidTest").ToString());
                    test.ValueExecution = float.Parse(this.conn.getDataString("value_").ToString());
                    test.OID=int.Parse(this.conn.getDataString("oid").ToString());
                    ((PlayerExecution)p.Current).Values.Add(test);
                }
            }            
            return list;
        }


    }
}
